home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / utils / timezone.el < prev    next >
Encoding:
Text File  |  1995-05-20  |  13.5 KB  |  388 lines

  1. ;;; timezone.el --- timezone package for GNU Emacs
  2. ;; Keywords: internal extensions news
  3.  
  4. ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  5.  
  6. ;;; Author: Masanobu Umeda
  7. ;;; Maintainer: umerin@mse.kyutech.ac.jp
  8.  
  9. ;; This file is part of XEmacs.
  10.  
  11. ;; XEmacs is free software; you can redistribute it and/or modify it
  12. ;; under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; XEmacs is distributed in the hope that it will be useful, but
  17. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. ;; General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  23. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25. ;; Modified 1 February 1994 by Kyle Jones to fix broken
  26. ;; timezone-floor function.
  27.  
  28. ;; Modified 25 January 1994 by Kyle Jones so that it will
  29. ;; work under version 18 of Emacs.  Provided timezone-floor
  30. ;; and timezone-abs functions.
  31.  
  32. ;;; Code:
  33.  
  34. (provide 'timezone)
  35.  
  36. (defvar timezone-world-timezones
  37.   '(("PST" .  -800)
  38.     ("PDT" .  -700)
  39.     ("MST" .  -700)
  40.     ("MDT" .  -600)
  41.     ("CST" .  -600)
  42.     ("CDT" .  -500)
  43.     ("EST" .  -500)
  44.     ("EDT" .  -400)
  45.     ("AST" .  -400)            ;by <clamen@CS.CMU.EDU>
  46.     ("NST" .  -330)            ;by <clamen@CS.CMU.EDU>
  47.     ("GMT" .  +000)
  48.     ("BST" .  +100)
  49.     ("MET" .  +100)
  50.     ("EET" .  +200)
  51.     ("JST" .  +900)
  52.     ("GMT+1"  .  +100) ("GMT+2"  .  +200) ("GMT+3"  .  +300)
  53.     ("GMT+4"  .  +400) ("GMT+5"  .  +500) ("GMT+6"  .  +600)
  54.     ("GMT+7"  .  +700) ("GMT+8"  .  +800) ("GMT+9"  .  +900)
  55.     ("GMT+10" . +1000) ("GMT+11" . +1100) ("GMT+12" . +1200) ("GMT+13" . +1300)
  56.     ("GMT-1"  .  -100) ("GMT-2"  .  -200) ("GMT-3"  .  -300)
  57.     ("GMT-4"  .  -400) ("GMT-5"  .  -500) ("GMT-6"  .  -600)
  58.     ("GMT-7"  .  -700) ("GMT-8"  .  -800) ("GMT-9"  .  -900)
  59.     ("GMT-10" . -1000) ("GMT-11" . -1100) ("GMT-12" . -1200))
  60.   "*Time differentials of timezone from GMT in +-HHMM form.
  61. This list is obsolescent, and is present only for backwards compatibility,
  62. because time zone names are ambiguous in practice.
  63. Use `current-time-zone' instead.")
  64.  
  65. (defvar timezone-months-assoc
  66.   '(("JAN" .  1)("FEB" .  2)("MAR" .  3)
  67.     ("APR" .  4)("MAY" .  5)("JUN" .  6)
  68.     ("JUL" .  7)("AUG" .  8)("SEP" .  9)
  69.     ("OCT" . 10)("NOV" . 11)("DEC" . 12))
  70.   "Alist of first three letters of a month and its numerical representation.")
  71.  
  72. (defun timezone-make-date-arpa-standard (date &optional local timezone)
  73.   "Convert DATE to an arpanet standard date.
  74. Optional 1st argument LOCAL specifies the default local timezone of the DATE;
  75. if nil, GMT is assumed.
  76. Optional 2nd argument TIMEZONE specifies a time zone to be represented in;
  77. if nil, the local time zone is assumed."
  78.   (let ((new (timezone-fix-time date local timezone)))
  79.     (timezone-make-arpa-date (aref new 0) (aref new 1) (aref new 2)
  80.                  (timezone-make-time-string
  81.                   (aref new 3) (aref new 4) (aref new 5))
  82.                  (aref new 6))
  83.     ))
  84.  
  85. (defun timezone-make-date-sortable (date &optional local timezone)
  86.   "Convert DATE to a sortable date string.
  87. Optional 1st argument LOCAL specifies the default local timezone of the DATE;
  88. if nil, GMT is assumed.
  89. Optional 2nd argument TIMEZONE specifies a timezone to be represented in;
  90. if nil, the local time zone is assumed."
  91.   (let ((new (timezone-fix-time date local timezone)))
  92.     (timezone-make-sortable-date (aref new 0) (aref new 1) (aref new 2)
  93.                  (timezone-make-time-string
  94.                   (aref new 3) (aref new 4) (aref new 5)))
  95.     ))
  96.  
  97.  
  98. ;;
  99. ;; Parsers and Constructors of Date and Time
  100. ;;
  101.  
  102. (defun timezone-make-arpa-date (year month day time &optional timezone)
  103.   "Make arpanet standard date string from YEAR, MONTH, DAY, and TIME.
  104. Optional argument TIMEZONE specifies a time zone."
  105.   (let ((zone
  106.      (if (listp timezone)
  107.          (let* ((m (timezone-zone-to-minute timezone))
  108.             (absm (if (< m 0) (- m) m)))
  109.            (format "%c%02d%02d"
  110.                (if (< m 0) ?- ?+) (/ absm 60) (% absm 60)))
  111.        timezone)))
  112.     (format "%02d %s %04d %s %s"
  113.         day
  114.         (capitalize (car (rassq month timezone-months-assoc)))
  115.         year
  116.         time
  117.         zone)))
  118.  
  119. (defun timezone-make-sortable-date (year month day time)
  120.   "Make sortable date string from YEAR, MONTH, DAY, and TIME."
  121.   (format "%4d%02d%02d%s"
  122.       year month day time))
  123.  
  124. (defun timezone-make-time-string (hour minute second)
  125.   "Make time string from HOUR, MINUTE, and SECOND."
  126.   (format "%02d:%02d:%02d" hour minute second))
  127.  
  128. (defun timezone-parse-date (date)
  129.   "Parse DATE and return a vector [YEAR MONTH DAY TIME TIMEZONE].
  130. 19 is prepended to year if necessary.  Timezone may be nil if nothing.
  131. Understands the following styles:
  132.  (1) 14 Apr 89 03:20[:12] [GMT]
  133.  (2) Fri, 17 Mar 89 4:01[:33] [GMT]
  134.  (3) Mon Jan 16 16:12[:37] [GMT] 1989
  135.  (4) 6 May 1992 1641-JST (Wednesday)
  136.  (5) 22-AUG-1993 10:59:12.82"
  137.   (let ((date (or date ""))
  138.     (year nil)
  139.     (month nil)
  140.     (day nil)
  141.     (time nil)
  142.     (zone nil))            ;This may be nil.
  143.     (cond ((string-match
  144.         "\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]*\\'" date)
  145.        ;; Styles: (1) and (2) without timezone
  146.        (setq year 3 month 2 day 1 time 4 zone nil))
  147.       ((string-match
  148.         "\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]*\\([-+a-zA-Z0-9]+\\)" date)
  149.        ;; Styles: (1) and (2) with timezone and buggy timezone
  150.        (setq year 3 month 2 day 1 time 4 zone 5))
  151.       ((string-match
  152.         "\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]+\\([0-9]+\\)" date)
  153.        ;; Styles: (3) without timezone
  154.        (setq year 4 month 1 day 2 time 3 zone nil))
  155.       ((string-match
  156.         "\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]+\\([-+a-zA-Z0-9]+\\)[ \t]+\\([0-9]+\\)" date)
  157.        ;; Styles: (3) with timezone
  158.        (setq year 5 month 1 day 2 time 3 zone 4))
  159.       ((string-match
  160.         "\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)[ \t]*\\([-+a-zA-Z0-9]+\\)" date)
  161.        ;; Styles: (4) with timezone
  162.        (setq year 3 month 2 day 1 time 4 zone 5))
  163.       ((string-match
  164.         "\\([0-9]+\\)-\\([A-Za-z]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)\\.[0-9]+" date)
  165.        ;; Styles: (5) without timezone.
  166.        (setq year 3 month 2 day 1 time 4 zone nil))
  167.       )
  168.     (if year
  169.     (progn
  170.       (setq year
  171.         (substring date (match-beginning year) (match-end year)))
  172.       ;; It is now Dec 1992.  8 years before the end of the World.
  173.       (if (< (length year) 4)
  174.           (setq year (concat "19" (substring year -2 nil))))
  175.       (setq month
  176.         (int-to-string
  177.          (cdr
  178.           (assoc
  179.            (upcase
  180.             ;; Don't use `match-end' in order to take 3
  181.             ;; letters from the beginning.
  182.             (substring date
  183.                    (match-beginning month)
  184.                    (+ (match-beginning month) 3)))
  185.            timezone-months-assoc))))
  186.       (setq day
  187.         (substring date (match-beginning day) (match-end day)))
  188.       (setq time
  189.         (substring date (match-beginning time) (match-end time)))))
  190.     (if zone
  191.     (setq zone
  192.           (substring date (match-beginning zone) (match-end zone))))
  193.     ;; Return a vector.
  194.     (if year
  195.     (vector year month day time zone)
  196.       (vector "0" "0" "0" "0" nil))
  197.     ))
  198.  
  199. (defun timezone-parse-time (time)
  200.   "Parse TIME (HH:MM:SS) and return a vector [hour minute second].
  201. Recognize HH:MM:SS, HH:MM, HHMMSS, HHMM."
  202.   (let ((time (or time ""))
  203.     (hour nil)
  204.     (minute nil)
  205.     (second nil))
  206.     (cond ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)\\'" time)
  207.        ;; HH:MM:SS
  208.        (setq hour 1 minute 2 second 3))
  209.       ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\'" time)
  210.        ;; HH:MM
  211.        (setq hour 1 minute 2 second nil))
  212.       ((string-match "\\`\\([0-9][0-9]\\)\\([0-9][0-9]\\)\\([0-9][0-9]\\)\\'" time)
  213.        ;; HHMMSS
  214.        (setq hour 1 minute 2 second 3))
  215.       ((string-match "\\`\\([0-9][0-9]\\)\\([0-9][0-9]\\)\\'" time)
  216.        ;; HHMM
  217.        (setq hour 1 minute 2 second nil))
  218.       )
  219.     ;; Return [hour minute second]
  220.     (vector
  221.      (if hour
  222.      (substring time (match-beginning hour) (match-end hour)) "0")
  223.      (if minute
  224.      (substring time (match-beginning minute) (match-end minute)) "0")
  225.      (if second
  226.      (substring time (match-beginning second) (match-end second)) "0"))
  227.     ))
  228.  
  229.  
  230. ;; Miscellaneous
  231.  
  232. (defun timezone-zone-to-minute (timezone)
  233.   "Translate TIMEZONE to an integer minute offset from GMT.
  234. TIMEZONE can be a cons cell containing the output of current-time-zone,
  235. or an integer of the form +-HHMM, or a time zone name."
  236.   (cond
  237.      ((consp timezone)
  238.       (/ (car timezone) 60))
  239.      (timezone
  240.       (progn
  241.     (setq timezone
  242.           (or (cdr (assoc (upcase timezone) timezone-world-timezones))
  243.           ;; +900
  244.           timezone))
  245.     (if (stringp timezone)
  246.         (setq timezone (string-to-int timezone)))
  247.     ;; Taking account of minute in timezone.
  248.     ;; HHMM -> MM
  249.     (let* ((abszone (timezone-abs timezone))
  250.             (minutes (+ (* 60 (/ abszone 100)) (% abszone 100))))
  251.        (if (< timezone 0) (- minutes) minutes))))
  252.      (t 0)))
  253.  
  254. (defun timezone-time-from-absolute (date seconds)
  255.   "Compute the UTC time equivalent to DATE at time SECONDS after midnight.
  256. Return a list suitable as an argument to current-time-zone,
  257. or nil if the date cannot be thus represented.
  258. DATE is the number of days elapsed since the (imaginary)
  259. Gregorian date Sunday, December 31, 1 BC."
  260.   (let* ((current-time-origin 719162)
  261.         ;; (timezone-absolute-from-gregorian 1 1 1970)
  262.      (days (- date current-time-origin))
  263.      (seconds-per-day (float 86400))
  264.      (seconds (+ seconds (* days seconds-per-day)))
  265.      (current-time-arithmetic-base (float 65536))
  266.      (hi (timezone-floor (/ seconds current-time-arithmetic-base)))
  267.      (hibase (* hi current-time-arithmetic-base))
  268.      (lo (timezone-floor (- seconds hibase))))
  269.      (and (< (timezone-abs (- seconds (+ hibase lo))) 2) ;; Check for integer overflow.
  270.       (cons hi lo))))
  271.  
  272. (defun timezone-time-zone-from-absolute (date seconds)
  273.   "Compute the local time zone for DATE at time SECONDS after midnight.
  274. Return a list in the same format as current-time-zone's result,
  275. or nil if the local time zone could not be computed.
  276. DATE is the number of days elapsed since the (imaginary)
  277. Gregorian date Sunday, December 31, 1 BC."
  278.    (and (fboundp 'current-time-zone)
  279.     (let ((utc-time (timezone-time-from-absolute date seconds)))
  280.       (and utc-time
  281.            (let ((zone (current-time-zone utc-time)))
  282.          (and (car zone) zone))))))
  283.  
  284. (defun timezone-fix-time (date local timezone)
  285.   "Convert DATE (default timezone LOCAL) to YYYY-MM-DD-HH-MM-SS-ZONE vector.
  286. If LOCAL is nil, it is assumed to be GMT.
  287. If TIMEZONE is nil, use the local time zone."
  288.   (let* ((date   (timezone-parse-date date))
  289.      (year   (string-to-int (aref date 0)))
  290.      (year     (if (< year 100) (+ year 1900) year))
  291.      (month  (string-to-int (aref date 1)))
  292.      (day    (string-to-int (aref date 2)))
  293.      (time   (timezone-parse-time (aref date 3)))
  294.      (hour   (string-to-int (aref time 0)))
  295.      (minute (string-to-int (aref time 1)))
  296.      (second (string-to-int (aref time 2)))
  297.      (local  (or (aref date 4) local)) ;Use original if defined
  298.      (timezone
  299.       (or timezone
  300.           (timezone-time-zone-from-absolute
  301.            (timezone-absolute-from-gregorian month day year)
  302.            (+ second (* 60 (+ minute (* 60 hour)))))))
  303.      (diff   (- (timezone-zone-to-minute timezone)
  304.             (timezone-zone-to-minute local)))
  305.      (minute (+ minute diff))
  306.      (hour-fix (timezone-floor minute 60)))
  307.     (setq hour (+ hour hour-fix))
  308.     (setq minute (- minute (* 60 hour-fix)))
  309.     ;; HOUR may be larger than 24 or smaller than 0.
  310.     (cond ((<= 24 hour)            ;24 -> 00
  311.        (setq hour (- hour 24))
  312.        (setq day  (1+ day))
  313.        (if (< (timezone-last-day-of-month month year) day)
  314.            (progn
  315.          (setq month (1+ month))
  316.          (setq day 1)
  317.          (if (< 12 month)
  318.              (progn
  319.                (setq month 1)
  320.                (setq year (1+ year))
  321.                ))
  322.          )))
  323.       ((> 0 hour)
  324.        (setq hour (+ hour 24))
  325.        (setq day  (1- day))
  326.        (if (> 1 day)
  327.            (progn
  328.          (setq month (1- month))
  329.          (if (> 1 month)
  330.              (progn
  331.                (setq month 12)
  332.                (setq year (1- year))
  333.                ))
  334.          (setq day (timezone-last-day-of-month month year))
  335.          )))
  336.       )
  337.     (vector year month day hour minute second timezone)))
  338.  
  339. ;; Partly copied from Calendar program by Edward M. Reingold.
  340. ;; Thanks a lot.
  341.  
  342. (defun timezone-last-day-of-month (month year)
  343.   "The last day in MONTH during YEAR."
  344.   (if (and (= month 2) (timezone-leap-year-p year))
  345.       29
  346.     (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
  347.  
  348. (defun timezone-leap-year-p (year)
  349.   "Returns t if YEAR is a Gregorian leap year."
  350.   (or (and (zerop  (% year 4))
  351.        (not (zerop (% year 100))))
  352.       (zerop (% year 400))))
  353.  
  354. (defun timezone-day-number (month day year)
  355.   "Return the day number within the year of the date month/day/year."
  356.   (let ((day-of-year (+ day (* 31 (1- month)))))
  357.     (if (> month 2)
  358.     (progn
  359.       (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
  360.       (if (timezone-leap-year-p year)
  361.           (setq day-of-year (1+ day-of-year)))))
  362.     day-of-year))
  363.  
  364. (defun timezone-absolute-from-gregorian (month day year)
  365.   "The number of days between the Gregorian date 12/31/1 BC and month/day/year.
  366. The Gregorian date Sunday, December 31, 1 BC is imaginary."
  367.   (+ (timezone-day-number month day year);; Days this year
  368.      (* 365 (1- year));;    + Days in prior years
  369.      (/ (1- year) 4);;        + Julian leap years
  370.      (- (/ (1- year) 100));;    - century years
  371.      (/ (1- year) 400)));;    + Gregorian leap years
  372.  
  373. (defun timezone-abs (n)
  374.   (if (fboundp 'abs)
  375.       (abs n)
  376.     (if (< n 0) (- n) n)))
  377.  
  378. (defun timezone-floor (n &optional divisor)
  379.   (if (fboundp 'floor)
  380.       (floor n divisor)
  381.     (if (null divisor)
  382.     (setq divisor 1))
  383.     (if (< n 0)
  384.     (- (/ (- divisor 1 n) divisor))
  385.       (/ n divisor))))
  386.  
  387. ;;; timezone.el ends here
  388.